package com.codegenerate.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.dto.SysDictionaryValueDto;
import com.codegenerate.demo.entity.SysDictionary;
import com.codegenerate.demo.entity.SysDictionaryValue;
import com.codegenerate.demo.service.impl.SysDictionaryServiceImpl;
import com.codegenerate.demo.service.impl.SysDictionaryValueServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/sysDictionary/sysDictionaryValue")
public class SysDictionaryValueController {
    @Autowired
    SysDictionaryValueServiceImpl sysDictionaryValueService;
    @Autowired
    SysDictionaryServiceImpl sysDictionaryService;


    @PostMapping("/list")
    R<List<SysDictionaryValue>> list(@RequestBody SysDictionaryValueDto sysDictionaryValueDto){
        Page<SysDictionaryValue> page = sysDictionaryValueService.page(
                sysDictionaryValueDto.buildPage(),sysDictionaryValueDto.dto2entity().getQueryAll()
        );
        return R.ok(page.getRecords())
                .setCount(page.getTotal());
    }

    @GetMapping("/getItem")
    R<SysDictionaryValue> getItem(int id) {
        return R.ok(sysDictionaryValueService.getById(id));
    }

    @PostMapping("/add")
    R<Boolean> add(@RequestBody  SysDictionaryValueDto sysDictionaryValueDto){
        var entity=sysDictionaryValueDto.dto2entity();
        //设置 默认
        SysDictionaryValue.setDefaultValue(entity);
        return R.ok(sysDictionaryValueService.save(entity));
    }


    @PostMapping("/update")
    R<Integer> update(@RequestBody  SysDictionaryValueDto sysDictionaryValueDto){
        sysDictionaryValueDto.validateUpdate();
        return R.ok(sysDictionaryValueService.getBaseMapper().update(null, sysDictionaryValueDto.dto2entity().getUpdateWrapper()));
    }

    @GetMapping("/del")
    R<Boolean> del(@RequestParam("id") int id) {
        return R.ok(sysDictionaryValueService.removeById(id));
    }

    @GetMapping("/getvaluelistbydiscode")
    R<List<SysDictionaryValue>> getvaluelistbydiscode(@RequestParam("code") String code) {
        //region: code 换id
        QueryWrapper<SysDictionary> queryDic = new QueryWrapper<SysDictionary>();
        queryDic.eq("code", code);
        var dicOnesResult=sysDictionaryService.page(new Page<>(1, 1), queryDic);
        //endregion

        //region 查询后校验
        if(dicOnesResult==null){
            return R.failed("没有查询到信息");}
        var dicOnes = dicOnesResult.getRecords();
        if(dicOnes==null){
            return R.failed("没有查询到信息");}
        var dicOne = dicOnes.get(0);
        if(dicOne==null){
            return R.failed("没有查询到信息");}
        //endregion


        //region: id 得到 dicvaluelist
        QueryWrapper<SysDictionaryValue> dicValueQueryWrapper = new QueryWrapper<SysDictionaryValue>();
        dicValueQueryWrapper.eq("dic_id", dicOne.getId());
        var pageResult=sysDictionaryValueService.page(new Page<SysDictionaryValue>(1, 100), dicValueQueryWrapper);
        //endregion


        //region: 查询后校验
        if(pageResult==null){
            return R.ok(Collections.emptyList());
        }
        //endregion

        return R.ok(pageResult.getRecords());
    }


}

